package com.yj.web.dao.mapper;

import com.yj.web.dao.po.admin.Banner;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by yanlinyang on 2017/7/24.
 */
@Component
public interface BannerMapper {
    @Select("<script>" +
            "select * from cus_banner " +
            "<trim prefix=\"where\" prefixOverrides=\"and | or\">" +
            "<if test = \"title != ''\">" +
            "instr(title, #{title}) " +
            "</if>" +
            "<if test = \"type != null\">" +
            "and type = #{type} " +
            "</if>" +
            "<if test = \"status != null\">" +
            "and status = #{status} " +
            "</if>" +
            "<if test = \"title != '-1'\">" +
            "and deleted_flag = 'N' " +
            "</if>" +
            "</trim>" +
            "order by position asc, created_at desc limit #{size} offset #{offset}" +
            "</script>")
    public List<Banner> listBanner(@Param("title") String title, @Param("type") Integer type, @Param("status") Integer status, @Param("offset") int offset, @Param("size") int size);

    @Select("<script>" +
            "select count(*) from cus_banner " +
            "<trim prefix=\"where\" prefixOverrides=\"and | or\">" +
            "<if test = \"title != ''\">" +
            "instr(title, #{title}) " +
            "</if>" +
            "<if test = \"type != null\">" +
            "and type = #{type} " +
            "</if>" +
            "<if test = \"status != null\">" +
            "and status = #{status} " +
            "</if>" +
            "<if test = \"title != '-1'\">" +
            "and deleted_flag = 'N' " +
            "</if>" +
            "</trim>" +
            "</script>")
    public int countBanner(@Param("title") String title, @Param("type") Integer type, @Param("status") Integer status);

    @Delete("update cus_banner set deleted_flag = 'Y' where id = #{id} and deleted_flag = 'N'")
    public int deleteBannerById(@Param("id") Integer id);

    @Insert("insert into cus_banner (create_id, created_at, updated_at, title, save_url, link_url, position, status, type) " +
            "values (#{banner.create_id}, now(), now(), #{banner.title}, #{banner.save_url}, #{banner.link_url}, #{banner.position}," +
            "#{banner.status}, #{banner.type})")
    @Options(useGeneratedKeys = true, keyProperty = "banner.id")
    public long insertBanner(@Param("banner") Banner banner);

    @Update("update cus_banner set update_id=#{banner.update_id}, updated_at=now(), title=#{banner.title}, " +
            " save_url=#{banner.save_url}, link_url=#{banner.link_url}, position=#{banner.position}, status=#{banner.status}, type=#{banner.type} where id = #{banner.id} and deleted_flag = 'N'")
    public boolean updateBanner(@Param("banner") Banner banner);

    @Select("select * from cus_banner where id = #{id} and deleted_flag = 'N'")
    public Banner findBannerById(@Param("id") Integer id);

    @Update("update cus_banner set status=#{status} where id = #{id}")
    public boolean updateBannerStatus(@Param("id") int  id, @Param("status") int  status);

    @Select("select max(position) from cus_banner where type = #{type}")
    public int getMaxPosition(@Param("type") int  type);
}
